//
//  AlarmClockAppDelegate.m
//  AlarmClock
//
//  Created by kai ren on 23/07/2011.
//  Copyright 2011 none. All rights reserved.
//

#import "AlarmClockAppDelegate.h"

@implementation AlarmClockAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize alarmListCtrl;
@synthesize alarms;
@synthesize audioPlayer;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    application.statusBarStyle = UIStatusBarStyleBlackOpaque;
	
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ];
	
	viewController = [[UITabBarController alloc] init];
	alarmListCtrl = [[ACAlarmListContoller alloc] init];
	
	UINavigationController *alarmListNavCtrl = [[UINavigationController alloc] initWithRootViewController:alarmListCtrl];
	alarmListNavCtrl.navigationBar.tintColor = barColor;
	
	SettingViewController *settingCtrl = [[SettingViewController alloc] initWithStyle:UITableViewStyleGrouped];
	UINavigationController *settingNavCtrl = [[UINavigationController alloc] initWithRootViewController:settingCtrl];
	settingNavCtrl.navigationBar.tintColor = barColor;
	
	viewController.viewControllers = [NSArray arrayWithObjects:alarmListNavCtrl, settingNavCtrl, nil];
	
	[alarmListNavCtrl release];	
	[settingCtrl release];
	
	self.alarms = alarmListCtrl.alarms;
		
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];
	
	NSLog(@"load");
	
    return YES;
}

- (void)palyAlarm:(ACAlarm *)alarm{
	if (audioPlayer != nil) {
		[audioPlayer stop];
		[audioPlayer release];
	}
	
	NSString *path = [[NSBundle mainBundle] pathForResource:alarm.sondName ofType:nil];
	
	NSLog(@"%@", path);
	audioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:path] error:nil];
	[audioPlayer play];
	
}

- (ACAlarm *)getAlarmWithNotification:(UILocalNotification *)notification{
	NSString *alarmID = [notification.userInfo objectForKey:ALARM_ID];
	ACAlarm *nowAlarm = nil;
		
	for (ACAlarm *alarm in self.alarms) {
		if ([alarm.alarmIdentifier isEqualToString:alarmID]) {
			nowAlarm = alarm;
		}
	}
		
	return nowAlarm;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	UIApplication *app = [UIApplication sharedApplication];
	NSArray *oldNotifications = [app scheduledLocalNotifications];
	NSDate *nowDate = [NSDate date];
	
	for (UILocalNotification *notification in oldNotifications) {
		if ([notification.fireDate timeIntervalSinceDate:nowDate] < 0) {
			ACAlarm *nowAlarm = [self getAlarmWithNotification:notification];
			
			if (nil == nowAlarm) {
				[[UIApplication sharedApplication] cancelLocalNotification:notification];
			} else if (!nowAlarm.alarmCtrl.isShowing) {
				[self palyAlarm:nowAlarm];
				nowAlarm.alarmCtrl.audioPlayer = self.audioPlayer;
				
				[self.viewController presentModalViewController:nowAlarm.alarmCtrl animated:YES];
				nowAlarm.alarmCtrl.isShowing = YES;
			}		
		}
	}
}

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
	NSLog(@"rec:%@\nnoti",notification.userInfo ,notification);
	ACAlarm *nowAlarm = [self getAlarmWithNotification:notification];
		
	if (nil == nowAlarm) {
		[[UIApplication sharedApplication] cancelLocalNotification:notification];
	} else {
		[self palyAlarm:nowAlarm];
		nowAlarm.alarmCtrl.audioPlayer = self.audioPlayer;
			
		if (!nowAlarm.alarmCtrl.isShowing) {
			[self.viewController presentModalViewController:nowAlarm.alarmCtrl animated:YES];
			nowAlarm.alarmCtrl.isShowing = YES;
		}		
	}
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end
